package com.zx.dao;

public class PaperImpl implements Paper {
    private int charPerLine = 16;   //每行字符数
    private int linePerPage = 5;    //每页的行数
    private String context = "";    //纸张中的内容
    private int posX = 0;
    private int posY = 0;
    private int posP = 1;

    @Override
    public void putInChar(char c) {
        context += c;
        ++posX;
        if (posX == charPerLine) {
            context += Paper.newline;
            posX = 0;
            ++posY;
        }

        if (posY == linePerPage) {
            context += "第" + posP + "页";
            context += Paper.newline + Paper.newline;
            posY = 0;
            ++posP;
        }
    }

    @Override
    public String getContext() {
        String ref = this.context;
        if (!(posX == 0 && posY == 0)) {
            int count = charPerLine - posY;
            for (int i = 0; i < count; i++) {
                ref += Paper.newline;
            }
            ref += "== 第" + posP + "页 ==";
        }
        return ref;
    }

    public void setCharPerLine(int charPerLine) {
        this.charPerLine = charPerLine;
    }

    public void setLinePerPage(int linePerPage) {
        this.linePerPage = linePerPage;
    }
}
